//
//  SpecialConversationListTableViewCell.m
//  BMHVideoMain
//
//  Created by mac on 2017/6/12.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "SpecialConversationListTableViewCell.h"

@implementation SpecialConversationListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self addOwnViews];
    }
    return self;
}

- (void)addOwnViews
{
    _conversationIcon = [[UIImageView alloc] init];
    _conversationIcon.backgroundColor = kWhiteColor;
    _conversationIcon.layer.cornerRadius = 4;
    _conversationIcon.layer.masksToBounds = YES;
    [self.contentView addSubview:_conversationIcon];
    
    _conversationName = [[UILabel alloc] init];
    _conversationName.font = kAppMiddleTextFont;
    _conversationName.textAlignment = NSTextAlignmentLeft;
    _conversationName.textColor = kBlackColor;
    [self.contentView addSubview:_conversationName];
    _lastmessage = [[UILabel alloc] init];
    _lastmessage.font = kAppSmallTextFont;
    _lastmessage.textColor = kGrayColor;
    [self.contentView addSubview:_lastmessage];

    
    ImageTitleButton *btn = [[ImageTitleButton alloc] init];
    btn.imageSize = CGSizeZero;
    btn.margin = UIEdgeInsetsMake(0, 0, 0, 0);
    btn.padding = CGSizeZero;
    _unReadBadge = btn;
    _unReadBadge.backgroundColor = HEXCOLOR(0xff4a45);
    _unReadBadge.layer.cornerRadius = 10;
    _unReadBadge.layer.masksToBounds = YES;
    _unReadBadge.titleLabel.font = kAppSmallTextFont;
    _unReadBadge.titleLabel.textAlignment = NSTextAlignmentCenter;
    _unReadBadge.titleLabel.adjustsFontSizeToFitWidth = YES;
    [_unReadBadge setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [self.contentView addSubview:_unReadBadge];
    
//    _enterIcon = [[UIImageView alloc] init];
//    _enterIcon.backgroundColor = kGrayColor;
//    [self.contentView addSubview:_enterIcon];

    _lastMsgTime = [[UILabel alloc] init];
    _lastMsgTime.font = kAppSmallTextFont;
    _lastMsgTime.textAlignment = NSTextAlignmentRight;
    _lastMsgTime.textColor = kLightGrayColor;
    _lastMsgTime.adjustsFontSizeToFitWidth = YES;
    [self.contentView addSubview:_lastMsgTime];

    
}
- (void)setHeadImageStr:(NSString *)imageStr withName:(NSString *)namestr withMessageNmber:(NSInteger)messageStr withlastmessge:(IMAMsg *)message{
    
    
    _conversationIcon.image = [UIImage imageNamed:imageStr];
    _conversationName.text = namestr;
    
    //NSDate *date = [message.msg timestamp];
    
    //_lastMsgTime.text = [date shortTimeDescriptionOfDate];
    //_lastmessage.text = @"官方消息发布专用账号";
    
    [_conversationIcon sizeWith:CGSizeMake(44, 44)];
    [_conversationIcon alignParentTopWithMargin:10];
    [_conversationIcon alignParentLeftWithMargin:kDefaultMargin];
    //    [_conversationIcon layoutParentVerticalCenter];
    
    
    
    if ( messageStr <= 9 &&  messageStr > 0)
    {
        _unReadBadge.hidden = NO;
//        [_unReadBadge sizeWith:CGSizeMake(20, 20)];
        _unReadBadge.frame = CGRectMake(XQScreenW - 28 - 8, 38, 20, 20);
    }else if (messageStr >= 10) {
        _unReadBadge.hidden = NO;
//        [_unReadBadge sizeWith:CGSizeMake(30, 20)];
        _unReadBadge.frame = CGRectMake(XQScreenW - 30 - 8, 38, 30, 20);

    }else {
        _unReadBadge.hidden = YES;
//        [_unReadBadge sizeWith:CGSizeMake(0, 0)];
        _unReadBadge.frame = CGRectMake(XQScreenW - 50, 22, 0, 0);

    }
    [_unReadBadge setTitle:messageStr > 99 ? @"99+" : [NSString stringWithFormat:@"%ld", messageStr] forState:UIControlStateNormal];
//    [_unReadBadge alignParentRightWithMargin:0];
//    [_unReadBadge alignParentTopWithMargin:22];

    
    NSLog(@"leathhhthh == %@",_unReadBadge.titleLabel.text);
    //    [_unReadBadge sizeWith:CGSizeMake(20, 20)];
    //
//    [_unReadBadge setHeight:20];
    [_conversationName sizeWith:CGSizeMake(150, 28)];
    [_conversationName layoutToRightOf:_conversationIcon margin:8];
//    [_conversationName scaleToLeftOf:_unReadBadge margin:8];
    if ([namestr isEqualToString:@"聊过的人"]) {
        [_conversationName alignParentTopWithMargin:(65-28)/2];
    }else{
        [_conversationName alignParentTopWithMargin:10];
    }
    [_lastmessage sameWith:_conversationName];
    [_lastmessage layoutBelow:_conversationName];
    [_lastmessage scaleToBelowOf:_conversationIcon];

    //[self updateCellOnNewMessage];
}
- (void)updateCellOnNewMessage
{
    
    
    
    NSInteger unReadCount = 0;
    _unReadBadge.hidden = unReadCount == 0;
    [_unReadBadge setTitle:unReadCount > 99 ? @"99+" : [NSString stringWithFormat:@"%ld", (long)unReadCount] forState:UIControlStateNormal];
    if (unReadCount < 10) {
        [_unReadBadge sizeWith:CGSizeMake(20, 20)];
    }else if (unReadCount > 9) {
        [_unReadBadge sizeWith:CGSizeMake(30, 20)];
        
    }
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self relayoutFrameOfSubViews];
}

- (void)relayoutFrameOfSubViews
{
    [_lastMsgTime sizeWith:CGSizeMake(60, 28)];
    [_lastMsgTime alignParentRightWithMargin:kDefaultMargin];
    [_lastMsgTime alignTop:_conversationIcon];

}

@end
